<template>
  <div>
    <!-- 面包导航区 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="#">商品管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区 -->
    <el-card>
      <!-- 头部搜索框 -->
      <el-row :gutter="20">
        <el-col span="8">
          <el-input clearable @clear='getGoodsList' placeholder="请输入内容" v-model="queryInfo.query">
            <el-button slot="append" icon="el-icon-search"
            @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col span="8">
          <el-button type="primary" @click="gotoadd">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 中间 商品列表table -->
      <el-table :data="goodsList"  border>
        <el-table-column type="index"></el-table-column>
        <el-table-column
          width="700px"
          label="商品名称"
          prop="goods_name"
        ></el-table-column>
        <el-table-column
          width="95px"
          label="商品价格"
          prop="goods_price"
        ></el-table-column>
        <el-table-column
          width="95px"
          label="商品重量"
          prop="goods_weight"
        ></el-table-column>
        <el-table-column width="150px" label="创建时间" prop="add_time">
          <template slot-scope="scope">
            {{ scope.row.add_time | formatDate }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              @click="removegood(scope.row)"
              type="warning"
              icon="el-icon-delete"
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 20, 30]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
  </div>
</template>


<script>
export default {
  data() {
    return {
      //查询的 info
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      //返回的商品列表的全部
      goodsList: [],
      //总和
      total: 0,

      //分页
      //当前页面
      currentPage4:1,

    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    //获取商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200)
        return this.$message.error("获取商品列表失败");
      this.$message.success("获取商品列表成功");
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    //pagesize变化
    handleSizeChange(val){
      this.queryInfo.pagesize = val
      this.getGoodsList()
    },
    //当前页面变化
    handleCurrentChange(val){
      this.queryInfo.pagenum = val
            this.getGoodsList()

    },

    //删除商品
    async removegood(info){
      const {data:res } =await this.$http.delete(`goods/${info.goods_id}`)
      if(res.meta.status!==200)return this.$message.error('删除失败')
      this.$message.success('删除成功')
      this.getGoodsList()
    },

    //去添加商品的页面
    gotoadd(){
      this.$router.push('/goods/add')
    }

  },
};
</script>

<style lang="less" scoped>
</style>